Pipe : Pipe communication

更新时间:
2024-05-14
下载文档

Pipe : Pipe communication

This module is a non-built-in multi-process pipe communication module. Only allow Privileged Mode system services to use. This module is not commonly used, for EdgerOS internal services only, you can ignore this chapter.

User can use the following code to import the Pipe module.

var Pipe = require('pipe');

Support

The following shows Pipe module APIs available for each permissions.

 User ModePrivilege Mode
Pipe 
pipe.fd 
pipe.name 
pipe.close 
pipe.read 
pipe.write 
pipe.autonomy 

Pipe Class

new Pipe(name, flags)

  • name {String} Pipe name.
  • flags {String} Create flags.
  • Returns: {Object} Pipe object.

Create a byte-oriented pipe object.

flagsDescription
rOpen or create a pipe for reading.
wOpen or create a pipe for writing.
rwOpen or create a pipe for reading and writing.

Since the kernel pipe file only has a read side, iosched will continuously detect readable events. To solve this problem, you can use 'rw' flags to open.

Example

var r = new Pipe('test1', 'r');
var w = new Pipe('test1', 'w');

Pipe Object

pipe.fd()

  • Returns: {Integer} Pipe file descriptor.

Get the pipe file descriptor, which can be asynchronous using with iosched module.

This file descriptor is only used for asynchronous event detection and is only used for current tasks.

pipe.name()

  • Returns: {String} Pipe file full name.

Get the full file name of the pipe, The full file name can be passed as a standard output parameter to the newly created process for intercepting the new process standard output.

Example

var r = new Pipe('test1', 'r');

// Pipe full name.
var stdout = r.name();
console.log(stdout);

process.spawn('./chlid.js', ['./chlid.js', '-o', `${stdout}`]);

pipe.close()

Close the pipe. If it is the standard output of other processes, do not close the pipe reader before the target process ends.

pipe.read(buffer[, offset[, length[, timeout]]])

  • buffer {Buffer} Receive buffer.
  • offset {Integer} Buffer offset. default:0.
  • length {Integer} Receive length limit. default:buffer.length.
  • timeout {Integer} Wait timeout in milliseconds. default: undefined means wait forever.
  • Returns: {Integer} The number of bytes actually receive, negative error.

To read the data of the pipe, you must ensure that the current pipeline is opened in read mode.

Example

var buffer = new Buffer(64);

// Read 64 bytes, return actually read number of bytes.
var num = pipe.read(buffer);

pipe.write(string[, timeout])

  • string {String} String to be send.
  • timeout {Integer} Wait timeout in milliseconds. default: undefined means wait forever.
  • Returns: {Integer} The number of bytes actually sent, negative error.

Write data to the pipe, you must ensure that the current pipe is opened in write mode.

Example

pipe.write('Test string');

pipe.write(buffer[, offset[, length[, timeout]]])

  • buffer {Buffer} Write data buffer.
  • offset {Integer} Buffer offset. default:0.
  • length {Integer} Write length. default:buffer.length.
  • timeout {Integer} Wait timeout in milliseconds. default: undefined means wait forever.
  • Returns: {Integer} The number of bytes actually sent, negative error.

Write data to the pipe, you must ensure that the current pipe is opened in write mode.

Example

var buf = new Buffer([1, 2, 3]);
pipe.write(buf);

pipe.autonomy(on)

  • on {Boolean} Whether to enable autonomy.
  • Returns: {Boolean} Whether the operation was successful.

Enabling or disabling autonomy, autonomy ensures that readable event detection does not depend on the presence of a writer.

文档内容是否对您有所帮助?
有帮助
没帮助